/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.m009.ui.views;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import ep.libs.implementations.AbstractModule;
import ep.libs.implementations.AbstractView;
import ep.m001.ui.views.LoginView;
import ep.m002.module.M002Module;
import ep.m009.ui.controllers.M009Controller;
import ep.m010.module.M010Module;

/**
 *
 * @author skyr
 */
public class M009View extends AbstractView {

    // <editor-fold defaultstate="collapsed" desc="CAPTIONS">
    public static final String CAPTION_PRESC_IDLE = "Εκκρεμείς συνταγές";
    public static final String CAPTION_AMKA = "ΑΜΚΑ";
    public static final String CAPTION_SUBMIT = "Υποβολή";
    public static final String CAPTION_CANCEL = "Άκυρο";
    public static final String CAPTION_BACK = "Επιστροφή";
    public static final String CAPTION_DOCTOR = "Στοιχεία Ιατρού";
    public static final String CAPTION_PATIENT = "Στοιχεία Ασφαλισμένου";
    public static final String CAPTION_DIAGNOSIS = "Διάγνωση";
    public static final String CAPTION_DOSAGE = "Θεραπεία";
    public static final String CAPTION_PRESC = "Στοιχεία Συνταγής";
    public static final String CAPTION_FIRST_NAME = "'Ονομα";
    public static final String CAPTION_SURNAME = "Επώνυμο";
    public static final String CAPTION_ADDRESS = "Διευθυνση";
    public static final String CAPTION_CITY = "Πόλη";
    public static final String CAPTION_TK = "ΤΚ";
    public static final String CAPTION_YEAR_OF_BIRTH = "Ημ/νια Γέννησης.";
    public static final String CAPTION_PHONE = "Τηλέφωνο";
    public static final String CAPTION_INSURANCE = "Ταμείο Ασφάλισης";
    public static final String CAPTION_FATHER_NAME = "Πατρώνυμο";
    public static final String CAPTION_AMETAA = "Α.Μ.Ε.Τ.Α.Α";
    public static final String CAPTION_MOB = "Κινητό";
    public static final String CAPTION_EMAIL = "Email";
    public static final String CAPTION_SPECIALTY = "Ειδικότητα";
    public static final String CAPTION_CHRONIC = "Χρόνια Πάθηση";
    public static final String CAPTION_PRESC_DATE = "Ημ/νια Έκδοσης";
    public static final String CAPTION_BEGIN_DATE = "Ημ/νια Έναρξης Συνταγής";
    public static final String CAPTION_END_DATE = "Ημ/νια Λήξης Συνταγής";
    public static final String CAPTION_DIAGNOSIS_COMMENTS = "Σχόλια";
    public static final String CAPTION_MEDICINE = "Περιγραφή Φαρμάκου";
    public static final String CAPTION_QUANTITY = "Ποσότητα";
    public static final String CAPTION_WAY_OF_INPUT = "Οδός χορήγησης";
    public static final String CAPTION_FREQUENCY = "Συχνότητα";
    public static final String CAPTION_DURATION = "Διαρκεια";
    public static final String CAPTION_PRESCRIBED = "Συνταγογραφήθηκε";// </editor-fold>
    private M009Controller controller;
    private Table prescTable;
    private Object patientsObject;
    private Button finalizeButton;
    private String[] prescStrings;
    private String[] prescHeaderStrings;

    public M009View(AbstractModule module) {
        super(module);
    }

    public M009View(AbstractModule module, Object[] args) {
        super(module, args);
        patientsObject = args[0];
    }

    @Override
    public ComponentContainer getView() {
        controller = new M009Controller(patientsObject);
        initializeFormStrings();
        initilizeComponents();
        initializeListeners();
        return viewLayout;
    }

    protected void initilizeComponents() {
        Panel mainPanel = new Panel(CAPTION_PRESC_IDLE);
        prescTable = new Table();
        prescTable.setImmediate(true);
        prescTable.setWidth("745px");
        prescTable.setSelectable(true);
        prescTable.setContainerDataSource(controller.getPrescDataSource());
        prescTable.setVisibleColumns(prescStrings);
        prescTable.setColumnHeaders(prescHeaderStrings);

        finalizeButton = new Button("Ολοκλήρωση");
        finalizeButton.setEnabled(false);
        Button backsButton = new Button("Επιστροφή");
        backsButton.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                module.getApplication().changeModule(M002Module.class, new Object[]{LoginView.PHARMACIST_TYPE, patientsObject});
            }
        });

        mainPanel.addComponent(prescTable);
        HorizontalLayout buttonLayout = new HorizontalLayout();
        buttonLayout.setSpacing(true);
        buttonLayout.addComponent(backsButton);
        buttonLayout.addComponent(finalizeButton);
        mainPanel.addComponent(buttonLayout);
        viewLayout.addComponent(mainPanel);
    }

    private void initializeListeners() {
        prescTable.addListener(new ValueChangeListener() {

            @Override
            public void valueChange(ValueChangeEvent event) {
                finalizeButton.setEnabled(prescTable.getValue() != null);
            }
        });

        finalizeButton.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                module.getApplication().changeModule(M010Module.class, new Object[]{controller.getPatient(), controller.getPresctriptionId(prescTable.getValue())});
            }
        });
    }

    private void initializeFormStrings() {
        prescStrings = new String[]{
                    "t002Doctor",
                    "t005Patient",
//                    "prescDate",
//                    "beginDate",
//                    "endDate",
//                    "chronic",
                    "t012Diagnosis",
                    "diagnosisComments"
                };
        prescHeaderStrings = new String[]{
                    CAPTION_DOCTOR,
                    CAPTION_PATIENT,
//                    CAPTION_PRESC_DATE,
//                    CAPTION_BEGIN_DATE,
//                    CAPTION_END_DATE,
//                    CAPTION_CHRONIC,
                    CAPTION_DIAGNOSIS,
                    CAPTION_DIAGNOSIS_COMMENTS
                };
    }
}
